package experimental;

import java.io.BufferedInputStream;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStream;
import java.io.StringReader;
import java.util.ArrayList;
import java.util.Collection;

import opennlp.tools.sentdetect.SentenceDetectorME;
import opennlp.tools.sentdetect.SentenceModel;
import opennlp.tools.tokenize.WhitespaceTokenizer;

import edu.stanford.nlp.tagger.maxent.MaxentTagger;

public class CorpusTagger {

	private static InputStream modelIn; 
	
	private static SentenceModel model;
	
	private static SentenceDetectorME sentenceDetector;
	/**
	 * @param args
	 * @throws IOException 
	 * @throws ClassNotFoundException 
	 */
	public static void main(String[] args) throws IOException, ClassNotFoundException {
		
		modelIn = new FileInputStream("en-sent.bin");
		
		model = new SentenceModel(modelIn);

		sentenceDetector = new SentenceDetectorME(model);
		
//		String corpus_dir = "/home/pjbarrio/Dataset/ACE2003/ace_phase2/data/train/";
		
		String corpus_dir = "/home/pjbarrio/Dataset/ACE2003/ace_phase2/data/ace2_devtest/test/";
		
		String corpus_list = "/home/pjbarrio/Dataset/ACE2003/ace_phase2/data/ace2_devtest/test/corpus_list";
		
		BufferedReader br = new BufferedReader(new FileReader(new File(corpus_list)));
		
		String file = br.readLine();

        // Initialize the tagger
        MaxentTagger tagger = new MaxentTagger(
                "/home/pjbarrio/Software/POSTagger/stanford-postagger-full-2011-06-19/models/left3words-wsj-0-18.tagger");
		
		while (file != null){
			
			String[] contentParagraph = readFile(new File(corpus_dir + "/" + file));
			
			String[] content = generateSentences(contentParagraph);
			
			String[] tagged = new String[content.length];
			
			for (int i = 0; i < content.length; i++) {
				
				tagged[i] = tagger.tagString(content[i]);
			
				tagged[i] = reformat(tagged[i]);
				
		        // Output the result
		        System.out.println(tagged[i]);
				
			}
			
			
	        write(corpus_dir,file.replace(".txt", ".pos"),tagged);
	        
			file = br.readLine();
			
		}

		br.close();
	}

	
	private static String reformat(String tagged) {
		
		tagged = tagged.replace("\\/", "");
		
		String tokenTags[] = WhitespaceTokenizer.INSTANCE.tokenize(tagged);
		
		String ret = "";
		
		for (int i = 0; i < tokenTags.length; i++) {
			
			int split = tokenTags[i].lastIndexOf("/");

			if ((split == -1)){
				
				tokenTags[i+1] = tokenTags[i] + tokenTags[i+1];
				
			} else {
				
				ret = ret + " " + tokenTags[i];
				
			}
			
		}
		
		return ret.substring(1);
		
	}


	private static String[] generateSentences(String[] contentParagraph) {
		
		ArrayList<String> ret = new ArrayList<String>();
		
		for (int i = 0; i < contentParagraph.length; i++) {
			
			ret.addAll(getSentences(contentParagraph[i]));
			
		}

		return ret.toArray(new String[0]);
		
	}

	private static Collection<? extends String> getSentences(String text) {
		
		ArrayList<String> ret = new ArrayList<String>();
		
		String sentences[] = sentenceDetector.sentDetect(text);
			
		for (int i = 0; i < sentences.length; i++) {

			ret.add(sentences[i]);

		}

		return ret;
		
	}

	private static void write(String corpus_dir, String file, String[] tagged) throws IOException {
		
		BufferedWriter bw = new BufferedWriter(new FileWriter(new File(corpus_dir + "/" + file)));
		
		for (int i = 0; i < tagged.length; i++) {
			
			bw.write("\n" + tagged[i] + "\n");
			
		}
		
		
		bw.close();
		
	}

	private static String[] readFile(File file) throws IOException {
		
		ArrayList<String> ret = new ArrayList<String>();
		
	    byte[] buffer = new byte[(int) file.length()];
	    BufferedInputStream f = null;
	    try {
	        f = new BufferedInputStream(new FileInputStream(file.getAbsolutePath()));
	        f.read(buffer);
	    } catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} finally {
	        if (f != null) try { f.close(); } catch (IOException ignored) { }
	    }
	    
	    String text = new String(buffer);
		
	    BufferedReader br = new BufferedReader(new StringReader(text));
	    
	    String line = br.readLine();
	    
	    String acum = "";
	    
	    while (line != null){
	    	
	    	if (line.trim().equals("")){
	    		
	    		
	    		if (!acum.trim().equals(""))
	    			ret.add(acum);
	    		
	    		acum = "";
	    	
	    	} else if (acum.trim().endsWith(".")){
	    	
	    		ret.add(acum);
	    		
	    		acum = line;
	    	
	    	}
	    	else {
	    		
	    		acum = acum + " " + line;
	    		
	    	}
	    	
	    	line = br.readLine();
	    }
	    
	    if (!acum.trim().equals("")){
	    	ret.add(acum);
	    }
	    
	    br.close();
	    
	    return ret.toArray(new String[0]);
	    
	}

}
